/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.datav.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.datav.entity.DataScreenCategory;
import com.jeeplus.modules.datav.service.DataScreenCategoryService;

/**
 * 大屏分类Controller
 * @author 刘高峰
 * @version 2021-03-29
 */
@RestController
@RequestMapping(value = "/datav/dataScreenCategory")
public class DataScreenCategoryController extends BaseController {

	@Autowired
	private DataScreenCategoryService dataScreenCategoryService;

	@ModelAttribute
	public DataScreenCategory get(@RequestParam(required=false) String id) {
		DataScreenCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dataScreenCategoryService.get(id);
		}
		if (entity == null){
			entity = new DataScreenCategory();
		}
		return entity;
	}


	/**
	 * 大屏分类树表数据
	 */
	@RequiresPermissions("datav:dataScreenCategory:list")
	@GetMapping("list")
	public AjaxJson list(DataScreenCategory dataScreenCategory) {
		return AjaxJson.success().put("list", dataScreenCategoryService.findList(dataScreenCategory));
	}

	/**
	 * 根据Id获取大屏分类数据
	 */
	@RequiresPermissions(value={"datav:dataScreenCategory:view","datav:dataScreenCategory:add","datav:dataScreenCategory:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(DataScreenCategory dataScreenCategory) {
		return AjaxJson.success().put("dataScreenCategory", dataScreenCategory);
	}

	/**
	 * 保存大屏分类
	 */
	@RequiresPermissions(value={"datav:dataScreenCategory:add","datav:dataScreenCategory:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(DataScreenCategory dataScreenCategory, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(dataScreenCategory);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		dataScreenCategoryService.save(dataScreenCategory);//保存
		return AjaxJson.success("保存大屏分类成功");
	}

	/**
	 * 删除大屏分类
	 */
	@RequiresPermissions("datav:dataScreenCategory:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			dataScreenCategoryService.delete (new DataScreenCategory (id));
		}
		return AjaxJson.success("删除大屏分类成功");
	}

	/**
     * 获取JSON树形数据。
     * @param extId 排除的ID
     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<DataScreenCategory> list = dataScreenCategoryService.findList(new DataScreenCategory());
		List rootTree =  dataScreenCategoryService.formatListToTree (new DataScreenCategory ("0"),list, extId );
		return AjaxJson.success().put("treeData", rootTree);
	}

}
